from rest_framework.pagination import PageNumberPagination
import logging

class ResultsSetPagination(PageNumberPagination):
    page_size = 50
    page_size_query_param = 'rows_per_page'
    max_page_size = 500
    page_query_param = 'page'

    def get_page_size(self, request):
        page_size = request.query_params.get(self.page_size_query_param)
        if page_size:
            return int(page_size)
        return self.page_size

def print_status_sorting(detail_objs, sort_seq):
    try:
        sort_dict = {
            "fgNum":'ori_fg_article',
            "-fgNum":'-ori_fg_article',
            "orderType":sort_seq, ## to be confirm
            "-orderType":sort_seq, ## to be confirm
            "orddate": 'ready_date',
            "-orddate": '-ready_date',
            "duedat": 'due_date',
            "-duedat": '-due_date',
            "designNum": 'design',
            "-designNum": '-design',
             "typcde": sort_seq, ## to be confirm
            "-typcde": sort_seq, ## to be confirm
        }
        if sort_seq in sort_dict:
            sort = sort_dict[sort_seq]
        else:
            sort = sort_seq

        logging.info(f"Sorting print status report param: {sort}")
        sorted_detail_objs = detail_objs.order_by(sort)
    except Exception as e:
        logging.error(f"Exception for sorting print status report: {e}")
        sorted_detail_objs = detail_objs
    return sorted_detail_objs
    

def leadtime_sorting(route_detail_objs, sort_seq):
    try:
        sort_dict = {
            "site":'order_route__workorder__site__name',
            "-site":'-order_route__workorder__site__name',
            "fgNum":'order_route__workorder__ori_fg_article', 
            "-fgNum":'-order_route__workorder__ori_fg_article',
            "duedat":'order_route__workorder__due_date', 
            "-duedat":'-order_route__workorder__due_date', 
            "designNum":'order_route__workorder__design', 
            "-designNum":'-order_route__workorder__design', 
            "pono":'order_route__workorder__order', 
            "-pono":'-order_route__workorder__order', 
            "podesc":'order_route__workorder__description', 
            "-podesc":'-order_route__workorder__description', 
            "goods_type":'order_route__workorder__goods_type', 
            "-goods_type":'-order_route__workorder__goods_type', 
            "ordqty":'order_route__workorder__quantity', 
            "-ordqty":'-order_route__workorder__quantity', 
            "complet":'order_route__workorder__complete_date', 
            "-complet":'-order_route__workorder__complete_date', 
            "ringType":'order_route__workorder__ring_type', 
            "-ringType":'-order_route__workorder__ring_type', 
             "readyDate":'order_route__workorder__ready_date', 
            "-readyDate":'-order_route__workorder__ready_date', 
            "mouldNo":'order_route__workorder__mould', 
            "-mouldNo":'-order_route__workorder__mould', 
            "flow":'flow__chinese_name', 
            "-flow":'-flow__chinese_name', 
 
        }
        if sort_seq in sort_dict:
            sort = sort_dict[sort_seq]
        else:
            sort = sort_seq

        logging.info(f"Sorting print status report param: {sort}")
        sorted_route_detail_objs = route_detail_objs.order_by(sort)
    except Exception as e:
        logging.error(f"Exception for sorting leadtime report: {e}")
        sorted_route_detail_objs = route_detail_objs
    return sorted_route_detail_objs
    
def transaction_sorting(record_objs, sort_seq):
    try:
        sorted_record_objs = record_objs.order_by(sort_seq)
    except Exception as e:
        logging.error(f"Exception for sorting qc transaction report: {e}")
        sorted_record_objs = record_objs
    return sorted_record_objs

def master_article_sorting(master_objs, sort_seq):
    try:
        sort_dict = {
            "gold":'gold_type',
            "-gold":'-gold_type',
            "article":'fg_article', ## to be confirm
            "-article":'-fg_article', ## to be confirm
        }
        if sort_seq in sort_dict:
            sort = sort_dict[sort_seq]
        else:
            sort = sort_seq

        # logging.info(f"Sorting print status report param: {sort}")
        sorted_master_objs = master_objs.order_by(sort)
    except Exception as e:
        logging.error(f"Exception for sorting qc transaction report: {e}")
        sorted_master_objs = master_objs
    return sorted_master_objs